package com.example.design.function;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

/**
 * 供给型接口
 */
public class TestSupplier {
    public static void main(String[] args) {
        //1.客户1 : 要5个1-10之间的随机数整数
        List<Integer> list1 = numSupplier(5, () -> (int) (Math.random() * 10 + 1));
        
        System.out.println(list1);
 
        //2.客户2 : 要8个1-100之间的偶数
        List<Integer> list2 = numSupplier(8, () -> {
            int num = (int) (Math.random() * 100 + 1);
            if (num % 2 == 0) {
                return num;
            } else {
                return num + 1;
            }
        });
        
        System.out.println(list2);
    }

    /**
     * 返回count个，满足用户定义的规律的数
     * @param count
     * @param supplier
     * @return
     */
    public static List<Integer> numSupplier(int count, Supplier<Integer> supplier) {
        List<Integer> res = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            Integer integer = supplier.get();
            res.add(integer);
        }
        return res;
    }
}